package com.yanxin.credit.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.yanxin.credit.dto.LoginDTO;
import com.yanxin.credit.dto.LoginUserDTO;
import com.yanxin.credit.utils.BeanUtils;
import com.yanxin.credit.utils.ServletUtils;
import com.yanxin.credit.vo.LoginVO;
import com.yanxin.credit.vo.WxErrorVO;
import com.yanxin.credit.vo.WxSessionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @program spring-cloud-example
 * @description:
 * @author: LiuYanXin
 * @create: 2022-02-09 15:55
 */
@Service
public class LoginService {


    @Autowired
    private TokenService tokenService;

    @Resource
    private AuthenticationManager authenticationManager;


    public LoginVO login(LoginDTO login) {

        // 用户验证
        Authentication authentication = null;
        // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
        authentication = authenticationManager
                .authenticate(new UsernamePasswordAuthenticationToken(login.getUsername(), login.getPassword()));
        LoginUserDTO loginUser = null;
        if (authentication.getPrincipal() instanceof LoginUserDTO) {
            loginUser = (LoginUserDTO) authentication.getPrincipal();
        }
        // 生成token
        return LoginVO.builder()
                .token(tokenService.createToken(loginUser))
                .build();
    }


    public WxSessionVO wxCode2Session(String code) {

        HttpResponse res = HttpUtil.createGet("https://api.weixin.qq.com/sns/jscode2session")
                .form("appid", "wxdc1f20499466eaa2")
                .form("secret","87842a29464dfd7e73c233bb1ca911b0")
                .form("js_code",code)
                .form("grant_type","authorization_code")
                .execute();
        if (res.isOk()) {
            String body = res.body();
            if (StrUtil.contains(body, "errcode")) {
                throw new BadCredentialsException(res.body());
            }
            return BeanUtils.strToObject(res.body(), WxSessionVO.class);
        }
        return null;
    }
}
